<template>
  <div class="search-history">
    <!-- 历史记录 -->

    <van-cell title="搜素历史">
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-search-history')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      :title="item"
      v-for="(item, index) in searchHistory"
      :key="index"
      @click="onHistoryClick(item, index)"
    >
      <van-icon v-show="isDeleteShow" name="close"></van-icon>
    </van-cell>

    <!-- /历史记录 -->
  </div>
</template>

<script>
export default {
  name: "SearchHistory",
  components: {},
  props: {
    searchHistory: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isDeleteShow: false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onHistoryClick(item, index) {
      // 如果是删除状态，则执行删除操作
      if (this.isDeleteShow) {
        this.searchHistory.splice(index, 1);
      } else {
        // 否则执行搜索操作
        // 利用子传父技术使用emit技术传递item的内容到父组件中；然后在父组件中触发onSearch事件然后搜索结果页面展示出来；
        this.$emit("search", item);
      }
    },
  },
};
</script>

<style scoped lang="less"></style>